package ocm.oh.ds;

public class State {
	private int violations;
	private int[] board;
	
	public State(int[] queens){
		initBoard(queens);
		calculateViolations();
	}

	public int getViolations() {
		return violations;
	}

	public int[] getQueens() {
		return board;
	}
	
	private void calculateViolations(){
		int count = 0;
		for(int i=0;i<board.length;i++){
			for (int j=i+1;j<board.length;j++){
				if (board[j] == board[i]) count+=2;
				else if(board[j] == board[i] + Math.abs(i-j)) count+=2;
				else if(board[j] == board[i] - Math.abs(i-j)) count+=2;
			}			
		}
		violations = count;
	}
	
	
//    private boolean isSolution() {
//        for(int row=0; row < queens.length; row++) {
//            if(getViolationsIfMovedHere(queens[row], row) > 0) return false;
//        }
//        return true;
//    }    
	
//    private int getViolationsIfMovedHere(int x, int queen) {
//        int count = 0;
//        for(int i=0; i < queens.length; i++) {
//            if(i != queen) {
//                if(queens[i] == x) count++;
//                else if(queens[i] == x + Math.abs(queen-i)) count++;
//                else if(queens[i] == x - Math.abs(queen-i)) count++;
//            }
//        }        
//        return count;
//    }
	
	
	private void initBoard(int[] queens){

		board = new int[queens.length];
		for (int i=0;i<queens.length;i++){
			board[i] = queens[i];
		}
	}

}
